package cn.jhc;

import java.io.IOException;
import org.apache.http.impl.bootstrap.HttpServer;
import org.apache.http.impl.bootstrap.ServerBootstrap;
import org.apache.http.protocol.HttpRequestHandler;

/**
 * A simple http server implemented by httpcore.
 * @author luyanfei
 */
public final class SimpleWebServer {
    private static final int PORT = 8080;

    private SimpleWebServer() {}

    /**
     * main entry point.
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        HttpRequestHandler rootHandler = new RootRequestHandler();
        HttpRequestHandler htmlHandler = new HtmlFileRequestHandler();

        final HttpServer httpServer = ServerBootstrap.bootstrap()
            .setListenerPort(PORT)
            .addInterceptorFirst(new LoggerInterceptor())
            .registerHandler("/", rootHandler)
            .registerHandler("/html/*", htmlHandler)
            .create();
        httpServer.start();
    }
}
